local DB = require("application.library.db")
local db = DB:new()
--local cjosn = require("cjson")
local cms_model = {}

--得到分类列表数据
function cms_model:cate_list(page, rows)
    local sql = "select * from m_category order by cateId desc limit ?,?"
    local param = { (page - 1) * rows, rows }
    local res, err = db:query(sql, param)
    if not res or err or type(res) ~= "table" or #res <= 0 then
        return {}
    else
        return res
    end
end

--得到分类列表总条数
function cms_model:cate_list_total()
    local sql = "select count(cateId) total from m_category  order by cateId desc"
    local res, err = db:query(sql)
    if not res or err or type(res) ~= "table" or #res <= 0 then
        return {}
    else
        return res
    end
end



--添加分类
function cms_model:cate_add(param)
    local sql = "INSERT INTO m_category (`cateId`, `cateName`, `catePath`, `cateSeoTitle`, `cateSeoStatus`, `cateKeywords`, `cateDescription`, `cateTemplate`, `weight`) VALUES (NULL,?, ?, ?, ?, ?, ?,?,?)"
    local res, err = db:insert(sql, {param.cate_name,param.cate_path,param.cate_seotitle,param.cate_seo,param.cate_keywords,param.cate_desc,param.cate_template,param.cate_weight})
    if not res or err then
        return { success = false, reason = "插入数据失败" }
    else
        return { success = true, result = "添加成功！" }
    end
end


--得到分类列表数据
function cms_model:cate_infos(id)
    local res, err = db:query("select * from m_category where cateId = ?", { id })
    if not res or err or type(res) ~= "table" or #res <= 0 then
        return {}
    else
        return res
    end
end

--修改分类信息
function cms_model:cate_update(param)
    local sql = "UPDATE  m_category  SET  cateName=?,catePath=?,cateSeoTitle=?,cateSeoStatus=?,cateKeywords=?,cateDescription=?,cateTemplate=?,weight=?  WHERE cateId = ?"
    local res, err = db:query(sql, param)
    if not res or err then
        return {
            success = false,
            msg = "更新栏目失败"
        }
    else
        return {
            success = true,
            msg = "更改栏目成功"
        }
    end
end


--查询文章列表
function cms_model:posts_list(page, rows,status,cateId)
    local sql = ""
    local param
    if cateId > 0 then
        sql = "SELECT tb1.*,tb2.cateName,tb2.catePath FROM `m_posts` as tb1 left join m_category as tb2 on tb1.cateid = tb2.cateId where tb1.status = ? and tb1.cateid = ? order by id desc limit ?,?"
        param = { status, cateId ,(page - 1) * rows, rows}
    else
        sql = "SELECT tb1.*,tb2.cateName,tb2.catePath FROM `m_posts` as tb1 left join m_category as tb2 on tb1.cateid = tb2.cateId where tb1.status = ? order by id desc limit ?,?"
        param = { status, (page - 1) * rows, rows }
    end

    local res, err = db:query(sql,param)
    if not res or err or type(res) ~= "table" or #res <= 0 then
        return {}
    else
        return res
    end
end



--查询文章列表总条数
function cms_model:posts_list_total(status,cateId)
    local sql = ""
    local param

    if cateId > 0 then
        sql = "SELECT count(tb1.id) total FROM `m_posts` as tb1 left join m_category as tb2 on tb1.cateid = tb2.cateId where tb1.status = ? and tb1.cateid = ? order by id desc"
        param = { status, cateId}
    else
        sql = "SELECT count(tb1.id) total  FROM `m_posts` as tb1 left join m_category as tb2 on tb1.cateid = tb2.cateId where tb1.status = ? order by id desc"
        param = {status}
    end
    --"SELECT count(tb1.id) total  FROM `m_posts` as tb1 left join m_category as tb2 on tb1.cateid = tb2.cateId where tb1.status = ? order by id desc", { status }
    local res, err = db:query(sql,param)
    if not res or err or type(res) ~= "table" or #res <= 0 then
        return {}
    else
        return res
    end
end


--按条件查询文章列表
--typeid 栏目id
--flag 标志，0正常1头条2推荐3图文
--rows返回条数
function cms_model:posts_list_by_type(typeid, flag, rows)
    --SELECT * FROM m_posts ORDER BY weight desc,id desc limit 2
    local sql = ""
    local param
    if typeid > 0 then
        sql = "SELECT * FROM m_posts where cateid = ? and flag = ?  ORDER BY weight desc,id desc limit ?"
        param = { typeid, flag, rows }
    else
        sql = "SELECT * FROM m_posts where  flag = ?  ORDER BY weight desc,id desc limit ?"
        param = { flag, rows }
    end

    local res, err = db:query(sql, param)
    if not res or err or type(res) ~= "table" or #res <= 0 then
        return {}
    else
        return res
    end
end


--添加文章
function cms_model:posts_add(param)
    local sql = "INSERT INTO m_posts (`cateid`, `title`, `author`, `weight`,`flag` ,`keywords`, `description`, `litpic`, `status`, `pubtime`,`uptime`,`click`,`content`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"
    local res, err = db:insert(sql, { param.cateid, param.title, param.author, param.weight,param.flag, param.keywords, param.description, param.litpic, 1, param.pubtime, param.uptime, param.click,param.content })
    if not res or err then
        return { success = false, reason = "添加文章失败" }
    else
        return { success = true, result = "添加文章成功！" }
    end

end

--得到文章详细
function cms_model:posts_infos(id)
    local res, err = db:query("select * from m_posts  where id = ?", { id })
    if not res or err or type(res) ~= "table" or #res <= 0 then
        return {}
    else
        return res
    end
end



--修改文章
function cms_model:posts_update(param)
    local sql = "UPDATE  m_posts  SET  cateid=?,title=?,author=?,weight=?,flag=?,keywords=?,description=?,litpic=?,click=?,content=?  WHERE id = ?"
    local res, err = db:query(sql, { param.cateid, param.title, param.author, param.weight,param.flag,param.keywords, param.description, param.litpic, param.click, param.content,param.id })
    if not res or err then
        return {
            success = false,
            msg = "修改文章失败"
        }
    else
        return {
            success = true,
            msg = "修改文章成功"
        }
    end
end


--更改文章状态
function cms_model:posts_set_status(status, id)
    local sql = "UPDATE  m_posts  SET status = ? WHERE id = ?"
    local res, err = db:query(sql, { status, id })
    if not res or err then
        return {
            success = false,
            msg = "操作失败"
        }
    else
        return {
            success = true,
            msg = "操作成功"
        }
    end
end



--网站设置
function cms_model:setting()
    local res, err = db:query("SELECT * FROM `m_website` where id = 1")
    if not res or err or type(res) ~= "table" or #res <= 0 then
        return {}
    else
        return res
    end
end



function cms_model:websetting(param)
    local sql = "UPDATE m_website SET webname = ?,domain = ?,beian = ?,banquan = ?,notice = ?,keywords = ?,description= ? WHERE id = 1"
    local res, err = db:insert(sql, { param.webname, param.domain, param.beian, param.banquan, param.notice, param.keywords, param.description})
    if not res or err then
        return {
            success = false,
            msg = "操作失败"
        }
    else
        return {
            success = true,
            msg = "操作成功"
        }
    end
end


function cms_model:search(words)

    local res, err = db:query("SELECT * FROM `m_posts` where title like ?",{words})
    if not res or err or type(res) ~= "table" or #res <= 0 then
        return {}
    else
        return res
    end
end


return cms_model